package com.yesallchina.wxapp.officialaccount.api.beans.param;


import com.yesallchina.wxapp.officialaccount.utils.JacksonUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


public class QrcodeParam implements Serializable {

    private final static int MAX_EXPIRE = 2592000;
    /**
     * 临时二维码有效时间：秒。最大2592000(30天)
     */
    private int expireSeconds;

    private QrcodeType qrcodeType;

    private String sceneId;

    private String sceneStr;

    public String getSceneId() {
        return sceneId;
    }

    public void setSceneId(String sceneId) {
        this.sceneId = sceneId;
    }

    public String getSceneStr() {
        return sceneStr;
    }

    public void setSceneStr(String sceneStr) {
        this.sceneStr = sceneStr;
    }

    public int getExpireSeconds() {
        return expireSeconds;
    }

    public void setExpireSeconds(int expireSeconds) {
        this.expireSeconds = expireSeconds;
    }

    public QrcodeType getQrcodeType() {
        return qrcodeType;
    }

    public void setQrcodeType(QrcodeType qrcodeType) {
        this.qrcodeType = qrcodeType;
    }

    public String toCreateQrcode() {
        Map json = new HashMap<>();
        json.put("action_name", getActionName(qrcodeType,sceneId,sceneStr));
        if (expireSeconds > 0) {
            if (expireSeconds > MAX_EXPIRE) {
                expireSeconds = MAX_EXPIRE;
            }
            json.put("expire_seconds", expireSeconds);
        }
        Map scene = new HashMap();
        if (StringUtils.hasText(sceneId)) {
            scene.put("scene_id", sceneId);
        } else {
            scene.put("scene_str", sceneStr);
        }
        Map actionInfo = new HashMap();
        actionInfo.put("scene", scene);
        json.put("action_info", actionInfo);
        return JacksonUtils.generate(json);
    }

    public String getActionName(QrcodeType qrcodeType, String senceId, String senceStr) {
        String tmpNum = "QR_SCENE";
        String tmpStr = "QR_STR_SCENE";
        String limitNum = "QR_LIMIT_SCENE";
        String limitStr = "QR_LIMIT_STR_SCENE";
        if (StringUtils.hasText(senceId)) {
            if (limitNum.contains(qrcodeType.getValue())) {
                return limitNum;
            }
            return tmpNum;
        }
        if (StringUtils.hasText(senceStr)) {
            if (limitStr.contains(qrcodeType.getValue())) {
                return limitStr;
            }
            return tmpStr;
        }
        return tmpStr;
    }

    public enum QrcodeType {
        PERMANENTLY(0, "TMP"),
        TEMPLATE(1, "LIMIT");
        private String value;
        private int code;

        QrcodeType(int code, String value) {
            this.value = value;
            this.code = code;
        }

        public String getValue() {
            return value;
        }

        public int getCode() {
            return code;
        }

        public static QrcodeType getByCode(int code) {
            for (QrcodeType type : QrcodeType.values()) {
                if (type.getCode() == code) {
                    return type;
                }
            }
            return QrcodeType.TEMPLATE;
        }
    }
}
